package com.yocard.yxyd.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yocard.yxyd.utils.util.ActivityUtils;
import com.yocard.yxyd.utils.util.TimeUtils;
import com.gyf.barlibrary.ImmersionBar;
import com.umeng.analytics.MobclickAgent;
import com.yocard.yxyd.R;
import com.yocard.yxyd.base.Constants;
import com.yocard.yxyd.http.model.response.CommonBean;
import com.yocard.yxyd.http.model.response.CsmPayInfo;
import com.yocard.yxyd.ui.activity.base.BaseActivity;
import com.yocard.yxyd.ui.contract.SettleContract;
import com.yocard.yxyd.ui.presenter.SettlePresenter;
import com.yocard.yxyd.utils.BigDecimalUtil;
import com.yocard.yxyd.utils.CommonUtils;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created on 2019/4/8.
 * 结算页面（已删除该步骤） 19/05/06
 */
public class SettlementActivity extends BaseActivity<SettlePresenter> implements SettleContract.View {

    protected String orderId;

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_amount)
    TextView tvAmount;
    @BindView(R.id.tv_periord)
    TextView tvPeriord;
    @BindView(R.id.tv_loan_type)
    TextView tvLoanType;
    @BindView(R.id.tv_1)
    TextView tv1;
    @BindView(R.id.tv_balance)
    TextView tvBalance;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.tv_big_price)
    TextView tvBigPrice;
    @BindView(R.id.ll_commit)
    LinearLayout llCommit;

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.act_settle;
    }

    @Override
    protected void initEventAndData() {
        initTitle();
        initData();
    }

    private void initData() {
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            orderId = extras.getString("id");
            showLoadingDialog();
            mPresenter.fetchOrderData(orderId);
        }
    }

    private void initTitle() {
        ImmersionBar.with(this).statusBarColor(R.color.white)
                .statusBarDarkFont(true).navigationBarColor(R.color.white).init();
        tvTitle.setText("结算");
    }

    @OnClick({R.id.iv_back, R.id.ll_commit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                ActivityUtils.finishActivity(this);
                break;
            case R.id.ll_commit:
                MobclickAgent.onEvent(mContext,"3","click_coin_pay");
                showLoadingDialog();
                mPresenter.doPay(orderId);
                break;
        }
    }

    @Override
    public void onFetchOrderData(CsmPayInfo csmOrderBean) {
        dismisDialog();
        try {
            tvName.setText(csmOrderBean.getSheets().getName());
            tvBigPrice.setText(BigDecimalUtil.getFormatCoin(csmOrderBean.getReal_coin_price()));
            tvPrice.setText(BigDecimalUtil.getFormatCoin(csmOrderBean.getReal_coin_price()) + "金币");
            tvTime.setText("申请时间：" + TimeUtils.millis2String(csmOrderBean.getSheets().getApplied_at() * 1000));
            tvAmount.setText(csmOrderBean.getSheets().getAmount());
            tvPeriord.setText(csmOrderBean.getSheets().getLoan_period());
            tvLoanType.setText(CommonUtils.getLoanTypeStr(csmOrderBean.getSheets().getLoan_type()));
            tvBalance.setText("(" + BigDecimalUtil.getFormatCoin(csmOrderBean.getCoin_balance()) + "金币)");
        }catch (Exception e){

        }
    }

    @Override
    public void onPaySuccess() {
        EventBus.getDefault().post(new CommonBean(Constants.CODE_REFRESH));
        EventBus.getDefault().post(new CommonBean(3));
        dismisDialog();
        Bundle bundle = new Bundle();
        bundle.putBoolean("isSuccess", true);
        bundle.putString("id",orderId);
        ActivityUtils.startActivity(bundle, SettleResultActivity.class);
        ActivityUtils.finishActivity(this);
    }

    @Override
    public void showErrorMsg(String msg) {
        super.showErrorMsg(msg);
        EventBus.getDefault().post(new CommonBean(Constants.CODE_REFRESH));
        Bundle bundle = new Bundle();
        bundle.putBoolean("isSuccess", false);
        bundle.putString("id",orderId);
        ActivityUtils.startActivity(bundle, SettleResultActivity.class);
        ActivityUtils.finishActivity(this);
    }
}
